<template>
	<view class="area flex flexC flexCol" :class="scrollIng ? 'hide' : ''">
		<image @click="open('door')" class="mt10 w48" src="/static/img/bg/doorbtn.png"></image>
		<image @click="open('room')" class="mt10 w48" src="/static/img/bg/roombtn.png"></image>
		<image @click="doXu" class="mt10 w48" src="/static/img/bg/xudanbtn.png"></image>
		<image class="mt10 w48" @click="tableOrder" src="/static/img/bg/openbtn.png"></image>
		<!-- 续费弹框 -->
		<uni-popup type="center" ref="popupXufei">
			<xufei v-if="yu != null" :item="yu" @xufeiPay="xufeiPay" @closeYuding="closeYuding"></xufei>
		</uni-popup>
		
		<uni-popup ref="timePop" type="bottom">
			<view class="popup-content2">
		
				<button v-for="item in xuList" class="popup-btn button"
					@click="clickXuOne(item)">{{item.order.room_name}}</button>
		
				<view class="popup-btn" @click="$refs.timePop.close()">取消</view>
			</view>
		
		</uni-popup>
	</view>

</template>

<script>
	const plugin = requirePlugin("myPlugin");

	export default {
		name: "slide-button",
		props: {
			scrollIng: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				yu: null,
				isPaying: false,
				xuList:[],
				isChooseComment:false
				
			};
		},
		mounted() {

		},
		methods: {
			
			clickXuOne(item){
				this.$refs.timePop.close();
				let param = {
					order_no:item.order.order_no
				};
				let that = this;
				this.$api.xu(param).then(res => {
					console.info(JSON.stringify(res));
					if (res.code == 1) {
						that.yu = res.data;
						this.$refs.popupXufei.open();
					
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}
				
				})
			},
			
			getXuFeiList(){
				
				let param = {
				
				};
				let that = this;
				this.$api.getXuList(param).then(res => {
					console.info(JSON.stringify(res));
					if (res.code == 1) {
				
						that.xuList = res.data;
						console.info(that.xuList);
						that.$refs.timePop.open();
					}
				
				})
			},

			getBtLockData(data) {


				let param = {
					shop_id: data.shop_id,
					room_id: data.room_id,
				}
				let that = this;
				this.$api.btLockInfo(param).then(res => {
					if (res.code == 1) {
						that.openLock(res.data);
					} else {

					}

				})


			},

			openLock(res) {


				uni.showLoading({
					title: "正在开启智能锁",
				})
				const start = Date.now();
				// 调用开锁接口


				var deviceId = res.lockMac;
				var lockData = res.lockData;
				plugin.controlLock(plugin.ControlAction.OPEN, lockData, res => {
					if (res.errorCode === 10003) {
						console.log("获取版本信息时设备连接已断开", res)
					}
				}, null, deviceId).then(res => {
					uni.hideLoading({});
					console.log(res)
					if (res.errorCode == 10012) {
						uni.showModal({
							title: "提示",
							content: "蓝牙未开启,请开启蓝牙",
							showCancel: false,

						})
					}
					if (!!res.deviceId) deviceId = res.deviceId;
					if (res.errorCode === 0) {
						console.info(`已开锁--操作时间:${Date.now() - start}ms.`)
					} else {

						console.info(`开锁失败: ${res.errorMsg}`)
					}
				})
			},

		
			xufeiPay(item) {
				
				let that = this;
				if(item.isComment == 1){
					
					
					uni.showModal({
						title:"提示",
						content:"您有一张免费的好评续时券，是否使用",
						confirmText:"使用",
						complete(res) {
							if(res.confirm){
								uni.navigateTo({
									url:'../discount/xuComment?id='+item.id
								})
							}else{
								that.requestXu(item);
							}
						}
					})
					return;
				}
				this.requestXu(item);
				
				
			},
			
			requestXu(item){
				let param = {
					id: item.id,
					hour: item.hour
				};
				
				if (this.isPaying == true) {
				
					return;
				}
				
				this.isPaying = true;
				let that = this;
				this.$api.xufeiPay(param).then(res => {
				
					console.info(JSON.stringify(res));
					that.isPaying = false;
					if (res.code == 1) {
						that.closeYuding();
						that.order_no = res.data.order_no;
						if (res.data.payInfo != '') {
							that.wechatPay(res.data.payInfo)
						} else {
							uni.redirectTo({
								url: '../order/detail?order_no=' + that.order_no
							})
						}
				
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}
				
				})
			},

			wechatPay(payConfig) {
				let that = this;
				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: payConfig.payTimeStamp,
					nonceStr: payConfig.paynonceStr,
					package: payConfig.payPackage,
					signType: payConfig.paySignType,
					paySign: payConfig.paySign,
					success: function(res) {

						uni.redirectTo({
							url: '../order/detail?order_no=' + that.order_no
						})

					},
					fail: function(err) {
						that.restore();
						uni.showToast({
							title: "取消续费",
							icon: 'none'
						});
					}
				});
			},

			closeYuding() {
				this.$refs.popupXufei.close();
			},

			tableOrder() {
				let param = {};
				let that = this;
				this.$api.checkTableOrder(param).then(res => {
					console.info(JSON.stringify(res));
					if (res.code == 1) {
						if (res.data != "") {
							uni.navigateTo({
								url: '../order/tableDetail?order_no=' + res.data
							})
						}
					} else {

					}

				})
			},
			doXu() {

				let param = {};
				let that = this;
				this.$api.xu(param).then(res => {
					console.info(JSON.stringify(res));
					if (res.code == 1) {
						
						if(res.data.count > 1){
							that.getXuFeiList();
						}else{
							that.yu = res.data;
							this.$refs.popupXufei.open();
						}
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						})
					}

				})
			},



			open(item) {
				let param = {
					type: item
				};
				let that = this;
				this.$api.open(param).then(res => {
					console.info(JSON.stringify(res));
					if (res.code == 1) {
						if (res.data.is_bt == 1) {
							that.getBtLockData(res.data);
						}

					}
					uni.showToast({
						icon: 'none',
						title: res.msg
					})
				})

			},

		}
	}
</script>

<style lang="scss" scoped>
	.area {
		z-index: 10;
		position: fixed;
		bottom: 10px;
		right: 10rpx;
		transition: right 0.5s ease-out; // 伸缩的动态效果

		.w48 {
			width: 96rpx;
			height: 96rpx;
		}

		.w52 {
			width: 102rpx;
			height: 102rpx;
		}

		&.hide {
			right: -104rpx;
		}
	}
	
	.popup-content2 {
		background: #FFFFFF;
	
		.popup-btn {
			width: 100%;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			border-bottom: 1px solid #f5f5f5;
			background: #FFFFFF;
		}
	
		.button {
			width: 100%;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			border-bottom: 1px solid #f5f5f5;
			background: #FFFFFF;
			font-size: 11pt;
		}
	
		button::after {
			border: none;
		}
	
		.pt20 {
			border-bottom: 20rpx solid #f5f5f5;
		}
	}
</style>